<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" :inline="true" label-width="68px">
              <el-form-item label="供应日期" style="width: 308px">
                <el-date-picker v-model="queryParams.riQi" type="date"
                                format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
              </el-form-item>

              <el-form-item label="档口" style="width: 308px">
                <el-select v-model="queryParams.dangKouId" placeholder="请选择">
                  <el-option v-for="item in dangKouList" :key="item.id" :label="item.name" :value="item.id" ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" icon="Grid" @click="handleExport">导出</el-button>
              </el-col>

              <el-col :span="1.5">
                <el-button type="primary" icon="Grid" @click="showShengChen = true">生成入库单</el-button>
              </el-col>
            </el-row>

            <el-table v-loading="loading" :data="caiGouDanList" @selection-change="tableSelect">
              <el-table-column type="selection" width="50" align="center"/>
              <el-table-column label="档口" align="center" prop="dangKouName"/>
              <el-table-column label="所属订单号" align="center" prop="dingDanId"/>
              <el-table-column label="订单详情号" align="center" prop="dingDanDetailId"/>
              <el-table-column label="供应日期" align="center" prop="riQi"/>
              <el-table-column label="供应时间" align="center" prop="shiJian"/>
              <el-table-column label="货品品类" align="center" prop="huoPinPinLeiName" width="120"/>
              <el-table-column label="货品编号" align="center" prop="huoPinId" width="120"/>
              <el-table-column label="货品" align="center" prop="huoPinName">
                <template #default="scope">
                  <span>{{ scope.row.huoPinName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="供应商" align="center" prop="gongYingShangName"/>
              <el-table-column label="货品价格" align="center" prop="huoPinPrice"/>
              <el-table-column label="采购数量" align="center" prop="buyCount"/>
              <el-table-column label="创建时间" align="center" prop="createTime" width="160">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize" @pagination="getList"/>
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <el-dialog :title="title" v-model="showShengChen" style="width: 80vw;" append-to-body>
      <el-form label-width="100px">

        <el-row>
          <el-col :span="24" style="padding: 10px 50px;">
            <el-table :data="selectRows">
              <el-table-column label="所属订单号" align="center" prop="dingDanId"/>
              <el-table-column label="订单详情号" align="center" prop="dingDanDetailId"/>
              <el-table-column label="货品" align="center" prop="huoPinName">
                <template #default="scope">
                  <span>{{ scope.row.huoPinName }}({{ scope.row.huoPinId }})</span>
                </template>
              </el-table-column>
              <el-table-column label="计划采购数量" align="center" prop="buyCount"/>
              <el-table-column label="实际采购数量" align="center" prop="realBuyCount">
                <template #default="scope">
                  <el-input v-model="scope.row.realBuyCount"  type="number" />
                </template>
              </el-table-column>
              <el-table-column label="计划采购价格" align="center" prop="huoPinPrice"/>
              <el-table-column label="实际采购价格" align="center" >
                <template #default="scope">
                  <el-input v-model="scope.row.realPrice"  type="number" />
                </template>
              </el-table-column>
              <el-table-column label="生产日期" align="center" >
                <template #default="scope">
                  <el-date-picker v-model="scope.row.shengChanRiQi" type="date"
                                  format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
                </template>
              </el-table-column>
              <el-table-column label="保质期" align="center" >
                <template #default="scope">
                  <el-input v-model="scope.row.baoZhiQi" />
                </template>
              </el-table-column>
              <el-table-column label="生产商" align="center" >
                <template #default="scope">
                  <el-input v-model="scope.row.shengChanShang" />
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="shengChengRuKuDan">保存修改</el-button>
          <el-button @click="showShengChen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="CaiGouDao">
import useAppStore from '@/store/modules/app'
import {isEmpty} from "@/utils/validate.js";
import {getToday} from "@/utils/time.js";
import {listPage, exportListUrl,} from "@/api/system/caigoudan.js";
import {listAll as loadAllDangKou} from "@/api/system/dangkou.js";
import {Splitpanes, Pane} from "splitpanes"
import "splitpanes/dist/splitpanes.css"
import {post} from "@/api/system/request.js";

const router = useRouter();
const appStore = useAppStore()
const {proxy} = getCurrentInstance();

const caiGouDanList = ref([]);
const loading = ref(true);
const total = ref(0);
const title = ref('');
const dangKouList = ref([]);

const form = ref({
  riQi: '',
  tongJiType: 'byTime',
  selectedPinLei: []
});
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  riQi: ""
});


function initDangKouList(){
  loadAllDangKou().then(res => {
    dangKouList.value = res.data;
  });
}

initDangKouList();

/** 查询用户列表 */
function getList() {

  loading.value = true;
  listPage(queryParams.value).then(res => {
    loading.value = false;
    caiGouDanList.value = res.rows;
    total.value = res.total;
  });
}

queryParams.value.riQi = getToday();

getList();


/** 搜索按钮操作 */
function handleQuery() {
  if(queryParams.value.riQi == null && queryParams.value.dangKouId == null){
    proxy.$modal.msgError("日期与档口请至少选择一个")
    return;
  }
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {};
}

const selectRows = ref([]);

function tableSelect(rows) {
  selectRows.value = rows;
  for(let row of selectRows.value) {
    row.realBuyCount = row.buyCount;
    row.realPrice = row.huoPinPrice;
  }
}

function handleExport() {
  proxy.download(exportListUrl, {
    ...queryParams.value,
  }, `${queryParams.value.riQi}-采购单信息.xlsx`);
}


const showShengChen = ref(false);
async function shengChengRuKuDan() {

  let rsp = await post("/system/rukudan/createByCaiGouDan", {list: selectRows.value});
  console.log(rsp);
  if (rsp.code === 200) {
    proxy.$modal.msgSuccess("已生成入库单");
    showShengChen.value = false;
  } else {
    proxy.$modal.msgWarning("生成入库单失败！");
  }
}


</script>
